package com.lqe.study.Session;

import com.lqe.study.Entity.Person;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.yaml.snakeyaml.DumperOptions;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;

@WebServlet("/SessionDemo02")
public class SessionDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        HttpSession session = req.getSession();

        String id = session.getId();
        System.out.println(id);

        long creationTime = session.getCreationTime();
        System.out.println(new Date(creationTime));

        Enumeration<String> attributeNames = session.getAttributeNames();
        while(attributeNames.hasMoreElements()){
            System.out.println(attributeNames.nextElement());
        }

        Person person1 = (Person)session.getAttribute("person1");
        Person person2 = (Person)session.getAttribute("person2");
        Person person3 = (Person)session.getAttribute("person3");

        System.out.println(person1);
        System.out.println(person2);
        System.out.println(person3);

        resp.getWriter().println(person1);
        resp.getWriter().println(person2);
        resp.getWriter().println(person3);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
